package cn.bluedot.framework.util;

import java.net.URL;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.bluedot.framework.dao.View;

public class XMLParser {
	
	private static Logger logger = LoggerFactory.getLogger(XMLParser.class);
	
	private final static String url = "/service.xml";
	
	private Document document; //XML文档对象
	private Element  configuration; //configuration结点
	private Element views; //views节点
	
	//单例
	private static XMLParser xmlParser = new XMLParser(url);
	
	private XMLParser() {}
		
	//返回XMLParser实例
	public static XMLParser getInstance(){
		return xmlParser;
	}
	
	private XMLParser(String fileUrl) {
		
		SAXReader reader = new SAXReader(); 

		//获取配置文件路径
		URL url = XMLParser.class.getResource(fileUrl);
		try {
			document = reader.read(url);
			configuration = document.getRootElement();
			views = configuration.element("views");
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	public String parseBasePackage() {
		Element element = configuration.element("auto-scan-package");
		return element.attributeValue("package").trim();
	}
	
	@SuppressWarnings("unchecked")
	public HashMap<String,View> parseViews() throws Exception{
		
		if(views == null){
			throw new Exception("views为空");
		}
		
		List<Element> list = views.elements();
		
		if(list == null) {
			throw new Exception("view为空");
		}
		
		HashMap<String,View> viewMapper = new HashMap<>();
		
		for (Element element : list) {
			
			View view = new View();
						
			String name = element.attributeValue("name");
			String method = element.attributeValue("method");
			
			view.setName(name);
			view.setMethod(method);
			
			logger.debug("[{}] add to view mapper",name);
			
			if(element.isTextOnly()) { //支持纯sql语句
				view.setOther(element.getText());
				viewMapper.put(name, view);
				continue;
			}
			
			Element table = element.element("table");
			String tableValue = table.getText();
						
			view.setTable(tableValue);
			
			
			List<Element> mappers = element.elements("mapper");
			
			if(mappers != null) {
				for (Element mapper : mappers) {
					String property = mapper.attributeValue("property");
					String column = mapper.attributeValue("column");
					HashMap<String,String> map = view.getMapper();
					map.put(property, column);
				}
			}
			
			Element condition = element.element("condition");
			
			if(condition != null){
				String conditionValue = condition.getText();
				view.setCondition(conditionValue);
			}
			
			List<Element> wheres = element.elements("where");
			if(wheres!=null){
				for (Element where : wheres) {
					String property = where.attributeValue("property");
					String column = where.attributeValue("column");
					
					HashMap<String,String> map = view.getWhere();
					map.put(property, column);
				}
			}
			
			Element other = element.element("other");
			
			if(other != null){
				String otherValue = other.getText();
				view.setOther(otherValue);
			}
		
			viewMapper.put(name, view);
		}
		
		return viewMapper;
	}
}